﻿package
{
    import fl.controls.DataGrid;
    import fl.controls.dataGridClasses.DataGridCellEditor;
    import fl.data.DataProvider;
    import fl.managers.StyleManager;
    import flash.display.Sprite;
    import flash.text.TextFormat;
    
    public class DataGridCellEditorExample extends Sprite
    {
        private var dg:DataGrid;
        
        public function DataGridCellEditorExample() {
            createDataGrid();
            styleDataGrid();
        }
        private function createDataGrid():void {
            dg = new DataGrid();
            addChild(dg);
            dg.setSize(200,300);
            dg.editable = true;
            
            var dp:DataProvider = new DataProvider();
            var i:uint;
            var totalRows:uint = 42;
            for(i=0; i<totalRows; i++) {
                dp.addItem( {     col1:getRandomNumber(),
                                   col2:getRandomNumber(),
                                col3:getRandomNumber() } );
            }
            dg.dataProvider = dp;            
        }
        private function styleDataGrid():void {
            var georgiaFormat:TextFormat = getCustomFormat();
            georgiaFormat.font = "Georgia";
            var verdanaFormat:TextFormat = getCustomFormat();
            verdanaFormat.font = "Verdana";
            var timesFormat:TextFormat = getCustomFormat();
            timesFormat.font = "Times New Roman";
            
            var firstCellEditor:DataGridCellEditor = getCustomEditor();
            firstCellEditor.setStyle("textFormat",timesFormat);
            var secondCellEditor:DataGridCellEditor = getCustomEditor();
            secondCellEditor.setStyle("textFormat",verdanaFormat);
            var thirdCellEditor:DataGridCellEditor = getCustomEditor();;
            thirdCellEditor.setStyle("textFormat",georgiaFormat);
            
            dg.columns[0].itemEditor = firstCellEditor;
            dg.columns[1].itemEditor = secondCellEditor;
            dg.columns[2].itemEditor = thirdCellEditor;    
        }
        private function getRandomNumber():uint {
            return Math.round(Math.random()*100);
        }
        private function getCustomFormat():TextFormat {
            var tf:TextFormat = new TextFormat();
            tf.bold = true;
            tf.color = 0xFFFFFF;
            return tf;
        }
        private function getCustomEditor():DataGridCellEditor {
            var dgce:DataGridCellEditor = new DataGridCellEditor();
            dgce.textField.background = true;
            dgce.textField.backgroundColor = 0xFF0000;
            dgce.maxChars = 2;
            dgce.restrict = "1234567890";
            return dgce
        }
    }
}